<?php
namespace waf\log;

use \waf\Waf;

class Log
{
    const LOG_LEVEL_SYSTEM  = 0;
    const LOG_LEVEL_ERROR   = 1;    //严重错误
    const LOG_LEVEL_WARNING = 2;    //警告
    const LOG_LEVEL_NOTICE  = 3;    //注意
    const LOG_LEVEL_INFO    = 4;    //流水
    const LOG_LEVEL_DEBUG   = 5;    //调试

    private static $_intLogLevel;
    private static $_strLogFile;
    private static $_arrLogInfo = [0 => 'SYSTEM', 1 => 'ERROR', 2 => 'WARNING', 3 => 'NOTICE', 4 => 'INFO', 5 => 'DEBUG'];

    public static function init()
    {
        $intLogLevel = intval(Waf::getConfig('log', 'log_level'));
        self::setLogLevel($intLogLevel);

        $strLogFile = Waf::getConfig('log', 'log_path').'/'.Waf::getConfig('log', 'log_file');
        self::setLogFile($strLogFile);
    }

    public static function setLogLevel($intLogLevel)
    {
        self::$_intLogLevel = $intLogLevel;
    }

    public static function setLogFile($strLogFile)
    {
        self::$_strLogFile = $strLogFile;
    }

    public static function doLog($intLogLevel, $logStr, $file, $line, $func, $r)
    {
        $file = basename($file);
        if($intLogLevel <= self::$_intLogLevel)
        {
            $strLog =   '['.date('Y-m-d H:i:s', time()).']' .
                '['.self::$_arrLogInfo[$intLogLevel].']' .
                "[file:{$file}]".
                "[line:{$line}]".
                "[func:{$func}]".
                "[r:{$r}]".
                '||'.$logStr.
                "\n";
            
            $strLogFile = $intLogLevel === self::LOG_LEVEL_SYSTEM ? self::$_strLogFile.'.system' : self::$_strLogFile .'.'. date('Ymd') ;
            error_log($strLog, 3, $strLogFile);
        }
    }

    public static function logError($logStr, $file = '', $line = '', $func = '', $r = 0)
    {
        self::doLog(self::LOG_LEVEL_ERROR, $logStr, $file, $line, $func, $r);
    }

    public static function logWarning($logStr, $file = '', $line = '', $func = '', $r = 0)
    {
        self::doLog(self::LOG_LEVEL_WARNING, $logStr, $file, $line, $func, $r);
    }

    public static function logNotice($logStr, $file = '', $line = '', $func = '', $r = 0)
    {
        self::doLog(self::LOG_LEVEL_NOTICE, $logStr, $file, $line, $func, $r);
    }

    public static function logInfo($logStr, $file = '', $line = '', $func = '', $r = 0)
    {
        self::doLog(self::LOG_LEVEL_INFO, $logStr, $file, $line, $func, $r);
    }

    public static function logDebug($logStr, $file = '', $line = '', $func = '', $r = 0)
    {
        self::doLog(self::LOG_LEVEL_DEBUG, $logStr, $file, $line, $func, $r);
    }

    public static function logSystem($logStr, $file = '', $line = '', $func = '', $r = 0)
    {
        self::doLog(self::LOG_LEVEL_SYSTEM, $logStr, $file, $line, $func, $r); 
    }
}
?>
